<?php

namespace App\Http\Controllers\Root;

use App\Http\Controllers\Controller;
use App\Http\Requests\UsersRequest;
use App\Repositories\UsersRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Session;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
use App\Libs\Jwt;

class UsersController extends Controller
{
    /** @var  UsersRepository */
    private $usersRepository;

    public function __construct(UsersRepository $usersRepo)
    {
        $this->usersRepository = $usersRepo;
    }


    public function login(Request $request)
    {
        $where = [];
        $username = $request->username;
        $pwd = $request->pwd;
        if (!empty($username)) {
            $where['username'] = $username;
        } else {
            return $this->sendError('用户名不能为空');
        }
        if (empty($pwd)) {
            return $this->sendError('密码不能为空');
        }
        $users = $this->usersRepository->findWhere($where, ['id', 'username', 'pwd']);
        if (count($users) == 0) {
            return $this->sendError('账号不存在或密码错误');
        }
        if (!empty($users)) {
            if (verifyPwd($pwd, $users[0]->pwd)) {
                $data['id'] = $users[0]->id;
                $data['name'] = $users[0]->username;
                session($data);
                return $this->sendResponse(1, '');
            } else {
                return $this->sendError('账号不存在或密码错误');
            }
        }
    }

    public function loginOut()
    {
        Session::flush();
        return redirect('/root/login');
    }


    /**
     * Display a listing of the Users.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $where = [];
        $order_by = [];
        $result = [];
        $this->usersRepository->pushCriteria(new RequestCriteria($request));
        $result = $this->usersRepository->getResult(15, $where, $order_by);
        return $this->r_view('root.users.index', ['users' => $result]);
    }


    /**
     * Store a newly created Users in storage.
     *
     * @param UsersRequest $request
     *
     * @return Response
     */
    public function store(UsersRequest $request)
    {
        $input = $request->all();
        if ($input['fb-id'] != 0) {
            $result = $this->usersRepository->findWithoutFail($input['fb-id']);
            if (empty($result)) {
                return $this->sendError('记录已删除');
            }
            $this->usersRepository->update($input, $input['fb-id']);
            return $this->sendResponse(1, '更新成功');
        } else {
            $this->usersRepository->create($input);
            return $this->sendResponse(1, '添加成功');
        }
    }

    /**
     * Display the specified Users.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $users = $this->usersRepository->findWithoutFail($id);

        if (empty($users)) {
            return redirect(route('users.index'));
        }

        return $this->r_view('root.users.show')->with('users', $users);
    }

    /**
     * Show the form for editing the specified Users.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $users = $this->usersRepository->findWithoutFail($id);

        if (empty($users)) {
            return redirect(route('users.index'));
        }

        return $this->r_view('root.users.edit')->with('users', $users);
    }


    /**
     * Remove the specified Users from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $users = $this->usersRepository->findWithoutFail($id);
        if (empty($users)) {
            return $this->sendError('该记录已删除');
        }
        $this->usersRepository->delete($id);
        return $this->sendResponse(1, '删除成功');
    }
}
